//初始化加载页面
$(function () {

    //判断是否点赞
    var isPraise = false;
    //是否在上拉加载中
    var loading = true;
    //当前页面
    var newPage = 0;
    appendDisplaypart();
    //下拉刷新页面
    $("#containerId").scroll(function () {
        var windowH = $(this).height();

        var pageH = $(this).find("div").eq(0).height();
        var scrollT = $(this).scrollTop();
        var toBottom = (pageH - scrollT);
        if (toBottom - windowH < 40) {

            if (loading) {
                loading = false;
                setTimeout(function () {
                    appendDisplaypart();
                }, 700);

            }
        }
    });
    /*关闭详情页面*/
    $('#clossAlertId').click(function () {
        closs(this);
    })
    /*
    * *************
    * 搜索
    * *************
    * */
    $('#search').click(function () {
        var textStr = $('#searchText').val();
        var pattren = /^[0-9]+$/;
        if (pattren.test(textStr)) {
            $.post('/checkdata.do', {
                "num": textStr
            }, function (data) {
                if (data.isexist) {
                    seeMore(textStr);
                } else {
                    message('没有找到ID为“' + textStr + '”的团队');
                }
            })
        } else {
            message("请输入正整数的ID");
        }
    });
    //增加人物框
    function appendDisplaypart() {
        $.post("/showInfo.do", {
            "page": newPage + 1
        }, function (data, statue) {
            if (statue == "success")  {
                loading = true;
                var container = $('#displayField');
                for (var x in data) {
                    var jsonData = data[x];
                    if (x == 0) {
                        newPage = jsonData.page;
                        isPraise = jsonData.praise;
                        if (jsonData.ifEndPage) {
                            loading = false;
                            $("#spinnerId").remove();
                            $("#containerId").append($('<div class=\"end\" id=\"endId\" > 没有更多了。。。</div>'));
                        }
                    } else {
                        var displayPart = $('<div class="displayPart clearfix" id="displayPart" ></div>');
                        var imgA = $('<a href="javascript:void(0)" sign="' + jsonData.number + '"><span class="name">' + jsonData.number + '&nbsp&nbsp' + jsonData.name + '</span><span class="go">查看详情>></span><div class="img1"><img src="..' + jsonData.picUrl + '" class="picture"></div></a>');
                        imgA.click(function () {
                            seeMore($(this).attr('sign'));
                        });
                        displayPart.append(imgA);
                        var commend = $('<div class="commend"></div>');
                        // console.log(jsonData.commended);

                        if (jsonData.commended) {
                            commend.append($('<div  class="like"><img src="./img/redHeart.png"><span>' + jsonData.commendNum + '</span></div>'));
                        } else {
                            var newDiv = $('<div sign="' + jsonData.number + '" id="' + jsonData.number + '" class="like"></div>');
                            newDiv.append($('<img src="./img/grayHeart.png"><span>' + jsonData.commendNum + '</span>'));

                            newDiv.one("click", function () {
                                like(this);
                            });
                            commend.append(newDiv);
                        }
                        displayPart.append(commend);
                        container.append(displayPart);
                    }
                }
            }else{
                    message("使用微信客户端才能点赞哟");
            }
        });
    }


    //查看详情
    function seeMore(firendId) {
        var alert = $('#alertField');
        alert.stop();
        alert.fadeIn(1000);

        $.post("/showMutilInfo.do", {
            "num": firendId
        }, function (data) {

            var container = $(".alertPart");
            var jsonData = data;
            var namePart = $('<div class="namePart">ID.' + jsonData.number + '&nbsp&nbsp' + jsonData.name + '</div>');
            var introduction = $('<div class="introduction"></div>');
            introduction.append($('<p class="theme">内容介绍</p>'));
            if (jsonData.commended) {
                introduction.append($('<div class="img"  ><span>' + jsonData.commendNum + '</span><img  src="./img/redHeart.png" ></div><p class="article">' + jsonData.introduction + '</p>'));
            } else {
                var newLike = $('<div class="img" sign="' + jsonData.number + '"><span>' + jsonData.commendNum + '</span><img src="./img/grayHeart.png" ></div>');
                //查看详情点赞
                newLike.one('click', function () {
                    like(this);
                });
                introduction.append(newLike);
                introduction.append($('<p class="article">' + jsonData.introduction + '</p>'));
            }
            var picturePart = $('<div class="picturePart"></div>');
            var imgSrc = "";
            for (var x in jsonData.picUrl) {
                imgSrc = imgSrc + '<img src="' + jsonData.picUrl[x] + '" alt="">';
            }
            picturePart.append($(imgSrc));
            container.append(namePart, introduction, picturePart);
        });
    }

    //关闭页面
    function closs(obj) {
        var alert = $("#alertField");
        alert.stop();
        alert.fadeOut(1000);
        $(".alertPart").html('');
    }
    /*
      * ****************
      * 消息框效果接口
      * 参数传入字符串{提示内容}
      * ****************
      * */
    function message(data) {
        $('#messageId').stop();
        $('#messageId').find('span').remove();
        $('#messageId').append($('<span>' + data + '</span>'));
        $('#messageId').animate({
            "top": "20px"
        }, 500, function () {
            setTimeout(function () {
                $('#messageId').animate({
                    "top": "-70px"
                }, 500);
            }, 1000);
        });

    }

    /*
    * **********
    * 点赞函数
    * **********
    * */
    function like(This) {
        if (isPraise) {
            message("今天已经点过赞了");
            return;
        }
        var firendId=$(This).attr('sign');
        $.post("/praise.do", {
            "num": firendId
        }, function (data) {
            if (data.isLike) {
                $(This).find("img").attr({
                    "src": "./img/redHeart.png"
                });
                $('#'+firendId).find('img').attr({
                    "src": "./img/redHeart.png"
                });
                if($(This).attr('id')!=firendId) {
                    $(This).find('span').text(Number($(This).find('span').text()) + 1);
                };

                $('#'+firendId).find('span').text(Number( $('#'+firendId).find('span').text()) + 1);
                message("点赞成功，今天不能点赞了");

                isPraise = true;
            } else {
                message("今天已经点过赞了");
            }
        })

    }

});

